/**
 * @file form reducer
 * @author liaoxuezhi@baidu.com
 */
/* eslint fecs-indent: [0, "space", 2, 2] */

import {
  UPDATE_FORM,
  CLEAR_FORM,

  REC_FETCH_FORM,
  REQ_FETCH_FORM,

  REC_FETCH_CHECK_FORM,
  REQ_FETCH_CHECK_FORM,

  REC_FETCH_FORM_SCHEMA,
  REQ_FETCH_FORM_SCHEMA,

  REQ_SAVE_FORM,
  REC_SAVE_FORM,

  REQ_FORM_ACTION,
  REC_FORM_ACTION,

  REQ_CHECK_FORM,
  REC_CHECK_FORM
} from './action-types';
import {handleActions} from 'redux-actions';
import * as update from 'react-addons-update';
import {compose, createRequestReducer, createReceiveReducer} from '../renderer';
import * as isPlainObject from 'lodash/isPlainObject';

export default handleActions({
  // [UPDATE_FORM]: (state, action) => update(state, {
  //   modified: {
  //     $set: action.payload.modified
  //   },
  //   data: {
  //     $set: action.payload.data
  //   }
  // }),

  // 获取数据相关
  [REQ_FETCH_FORM]: createRequestReducer(),
  [REC_FETCH_FORM]: compose((state, action) => action.error ? state : update(state, {
    data: {
      [/*state.data ? '$merge' : */'$set']: action.payload.data || null
    }
  }), createReceiveReducer()),

  // 获取数据相关
  [REQ_FETCH_CHECK_FORM]: compose(state => update(state, {
    initFinished: {
      '$set': false
    }
  }), createRequestReducer('initChecking')),
  [REC_FETCH_CHECK_FORM]: compose((state, action) => action.error ? state : update(state, {
    data: {
      [/*state.data ? '$merge' : */'$set']: action.payload.data || null
    },
    initFinished: {
      $set: !!action.payload.data && action.payload.data[action.meta.meta.initFinishedField]
    }
  }), createReceiveReducer('initChecking')),


  // 保存数据相关
  [REQ_SAVE_FORM]: compose((state, action) => update(state, {
    data: {
      [state.data ? '$merge' : '$set']: action.payload
    }
  }), createRequestReducer('saving')),
  [REC_SAVE_FORM]: compose((state, action) => action.error || !isPlainObject(action.payload.data)
    ? state : update(state, {
    data: {
      [state.data ? '$merge' : '$set']: action.payload.data || null
    }
  }), createReceiveReducer('saving')),


  // 其他 action 操作
  [REQ_FORM_ACTION]: compose((state, action) => update(state, {
      data: {
        [state.data ? '$merge' : '$set']: action.payload
      }
    }),
    createRequestReducer('saving', 'action')
  ),
  [REC_FORM_ACTION]: compose((state, action) => action.error || !isPlainObject(action.payload.data)
    ? state : update(state, {
    data: {
      [state.data ? '$merge' : '$set']: action.payload.data || null
    }
  }), createReceiveReducer('saving', 'action')),

  // 获取数据相关
  [REQ_FETCH_FORM_SCHEMA]: createRequestReducer('fetching', 'schema'),
  [REC_FETCH_FORM_SCHEMA]: compose((state, action) => action.error ? state : update(state, {
    schema: {
      data: {
        [state.schema.data ? '$merge' : '$set']: action.payload.data || null
      }
    }
  }), createReceiveReducer('fetching', 'schema')),


  // 检查 form 是否完成
  [REQ_CHECK_FORM]: compose(state => update(state, {
    finished: {
      '$set': false
    }
  }), createRequestReducer('checking')),
  [REC_CHECK_FORM]: compose((state, action) => action.error ? state : update(state, {
    finished: {
      '$set': !!action.payload.data && action.payload.data[action.meta.meta.finishedField]
    }
  }), createReceiveReducer('checking')),


  [CLEAR_FORM]: (state, action) => update(state, {
    $merge: {
      data: null
    }
  })
}, {
  ctx: null,
  message: null,
  error: null,
  invalid: false,
  fetching: false,
  saving: false,
  data: null,
  // modified: false,

  checking: false,
  finished: false,

  initChecking: false,
  initFinished: false,

  // 其他 ajax 操作时, 记录状态
  action: {
    ctx: null,
    message: null,
    saving: false,
    invalid: false,
    error: null
  },

  schema: {
    data: null,
    ctx: null,
    message: null,
    saving: false,
    invalid: false,
    error: null
  }
});